<?php
class Security_directory extends Controller {
    var $company_id;
    var $countryList;
    function Security_directory()
    {
        parent::Controller();
        
        $this->load->library('session');
		 if($this->session->userdata('admin') == '')
		 {
			 redirect('admin');
		 }
        $this->load->model(array('Directory_company_model','User_admin_model','Directory_services_model',
        'Directory_user_model','Directory_photo_model'));
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation','pagination');
        $this->countryList = $this->db->get('country');    
    }
    
    function index(){ 
        
        
       /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"]))
       {
            redirect('../admin');
       } */
       if($this->input->post('submit')){            
            $list_company = $this->input->post('company');
            foreach($list_company AS $company_id){                
                $data['delete_query'] = $this->Directory_company_model->delete_company($company_id);
            }
        }
        $param_default = array(order,page);
        $param_array = $this->uri->uri_to_assoc(4,$param_default);
        //echo "<pre><font color='#ccc'>";print_r($param_array);echo "</font></pre>";
        //echo "<pre><font color='#ccc'>";print_r($order);echo "</font></pre>";
        if($this->input->post('order_by')){
            $order = $this->input->post('order_by');    
        }else if(!empty($param_array['order'])){
            $order = $param_array['order'];
        }else{
            $order = 'date_desc';
        }        
        
        $this->load->library('pagination');
        if($param_array['order'] == ''){
            $param_order = 'company_asc';    
        }else{
            $param_order = $param_array['order'];
        }
        
        if($param_array['status'] == ''){
            $status = 'active';
            $data['active_user'] = 1;    
        }else{
            $status = $param_array['status'];
            $data['inactive_user'] = 1;
        }
        $data['count'] = $this->Directory_company_model->get_count_company_order($order, $status);
        $offset = $param_array['page'];//$this->uri->segment(5);
         
        //$config['base_url'] = site_url('/admin/security_directory/index/order/'.$param_array['order'].'/page/'.$param_array['page']);
        $config['base_url'] = site_url('/admin/security_directory/index/status/'.$status.'/order/'.$order.'/page/');
        $config['total_rows'] = $this->Directory_company_model->get_count_company_order($order, $status);
        $config['uri_segment'] = 9;//$param_array['page']; //5; 
        $config['per_page'] = '10';        

        // First Links
        $config['first_link'] = 'First';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';

        // Last Links
        $config['last_link'] = 'Last';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        // Next Link
        $config['next_link'] = 'Next &raquo;';
        $config['next_tag_open'] = '<li class="next">';
        $config['next_tag_close'] = '</li>';

        // Previous Link
        $config['prev_link'] = '&laquo; Previous';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';

        // Current Link
        $config['cur_tag_open'] = '<li class="active">';
        $config['cur_tag_close'] = '</li>';

        // Digit Link
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();
        $data['list_company'] = $this->Directory_company_model->get_list_company_order($order, 10, $offset, $status);
        $data['status'] = $status;
        $data['order'] = $order;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($_POST);echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/admin/directory/company_list', $data);
    }
    
    function services(){
    /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"]))
       {
            redirect('../admin');
        }*/
        
        $service_type = $this->input->post('types');
        $param_array = $this->uri->uri_to_assoc(4);
        $category = $param_array['category'];
        switch($category){
            case 'products' : $data['products_on'] = 1; break;
            case 'services' : $data['services_on'] = 1; break;
            case 'brands' : $data['brands_on'] = 1; break;
        }
        $serv_id = $param_array['id'];
        if($this->input->post('submit') == 'submit'){
            $services_name = $this->input->post('services_name');
            $services_id = $this->input->post('services_id');
            if($services_id != ''){
                $data['debug'] = $this->Directory_services_model->update_services($category,  $service_type, $services_name, $services_id);    
            }else{                
                $data['debug'] = $this->Directory_services_model->add_services($category, $service_type, $services_name);    
            }         
        }
        if($serv_id){
            $data['debug'] = $this->Directory_services_model->delete_services($serv_id);    
        }
        
        $data['list_services'] = $this->Directory_services_model->get_list_services_with_usage($category);
        $data['category'] = $category;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($param_array); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($_POST); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/admin/directory/services_list', $data);    
    }
    
    function activation(){
        
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $invoice_id = $param_array['invoice_id'];        
        
        if($this->input->post('submit') == 'activate'){
            $activation = $this->input->post('activation');
            $this->Directory_company_model->set_company_activation($id, $activation);
            if($activation == 'activate'){
                redirect('admin/security_directory');    
            }else{
                redirect('admin/security_directory/index/status/inactive');
            }    
        }
        if($this->input->post('submit') == 'notes'){
            $text = $this->input->post('admin_notes');
            $this->Directory_company_model->set_admin_note($id, $text);
            $data['notification']="Admin note has been updated";    
        }
        if($this->input->post('submit') == 'manual_payment'){
            $this->form_validation->set_rules('total_paid', 'Amount Paid','required');
            $this->form_validation->set_rules('payment_method', 'Paid via','required');
            $this->form_validation->set_rules('expiry_date', 'Expiry date','required');
            if($this->form_validation->run() == TRUE){                
                $paid_until = $this->input->post('expiry_date');
                $total_paid = $this->input->post('total_paid');
                $payment_in = $this->input->post('payment_method');                
                $data_user = $this->Directory_user_model->get_detail($id);
                $data['debug'] = $this->Directory_invoice_model->insert_invoice($id, $data_user['company'], 
                    $data_user['first_name'] . ' ' .$data_user['surename'], $data_user['telephone'], 
                    'turnitaround admin', date('Y-m-d H:i:s'),date('Y-m-d H:i:s'), $payment_in, '', '', $total_paid, 'paid', $paid_until,'0');
                
                $data['notification']="Manual payment has been updated to this company account";
                $this->Directory_company_model->set_company_activation($id);
            }    
        }   
        if($this->input->post('submit') == 'delete_invoice'){
            //die($invoice_id." -- here");
            $this->Directory_invoice_model-> delete_invoice($invoice_id);
            $data['notification']="Invoice has been deleted";    
        }                        
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['company_invoice'] = $this->Directory_invoice_model->get_company_invoice($id);
        //echo $this->Directory_invoice_model->db->last_query();
        //die(print_r($data['company_invoice']));
        $data['activation'] = 1;
        
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($param_array); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($_POST); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_activation', $data);    
    }
    
    function login_detail(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $this->company_id = $id;
        $check_user = $this->Directory_user_model->get_detail($id);
        
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $re_password = $this->input->post('re_password');
            if($check_user['email'] != $email){
                $this->form_validation->set_rules('email', 'e-mail','required|valid_email|callback_check_email');    
            }            

            //if($password != "" || $re_password != ""){
              $this->form_validation->set_rules('password', 'Password','required|min_length[6]');
              $this->form_validation->set_rules('re_password', 're-typed Password','required|min_length[6]|matches[password]');
            //}
            if($this->form_validation->run() == TRUE){
              $this->Directory_user_model->update_login_info($id, $email, $password);
              $data['notification']="Login information has been updated";
            }

        }
        
        $user_detail = $this->Directory_user_model->get_detail($id);
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['login_detail'] = 1;
        $data['email'] = $user_detail['email'];
        $data['real_pass'] = mask_password($user_detail['password_real'],"*",3);        
    
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_login', $data);    
    }
    
    function statistics()
    {
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $data['company'] = $this->Directory_company_model->get_company_detail($id); 
        $data['statistic'] = 1;
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_statistics', $data);   
    }
    
    function company_detail(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        
        $data['company_detail'] = 1;
        
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $this->form_validation->set_rules('name', 'Company name','required');
            $this->form_validation->set_rules('description', 'Company Description','required');
            $this->form_validation->set_rules('contact_number', 'Telephone number','required');

            $this->form_validation->set_rules('contact_email', 'Contact email','valid_email'); 
            $this->form_validation->set_rules('physical_street', 'Physical street address','required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address','required');
            $this->form_validation->set_rules('physical_city', 'Physical city address','required');
            //$this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            $this->form_validation->set_rules('province_id', 'Province','required');
            //$this->form_validation->set_rules('city_name', 'City','required');            
            if($this->form_validation->run() == TRUE){
            
              $company_name = ($this->input->post('name'));
              $company_desc = $this->input->post('description');
              if($this->input->post('website') == 'http://'){
                  $company_website = '';
              }else{
                  $company_website = substr($this->input->post('website'), 7);
              }
              //$company_website = $this->input->post('website');
              $contact_number = $this->input->post('contact_number');
              $mobile = $this->input->post('mobile_number');
              $fax_number = $this->input->post('fax_number');
              //$contact_person = $this->input->post('contact_person');
              $contact_email = $this->input->post('contact_email');
              //add province name and city name 
              $province_id = $this->input->post('province_id');
              $city_name = $this->input->post('city_name');
              // location data
              $physical_street = $this->input->post('physical_street');
              $physical_suburb = $this->input->post('physical_suburb');
              $physical_city = $this->input->post('physical_city');
              $physical_zipcode = $this->input->post('physical_zipcode');
              $postal_street = $this->input->post('postal_street');
              $postal_suburb = $this->input->post('postal_suburb');
              $postal_city = $this->input->post('postal_city');
              $postal_zipcode = $this->input->post('postal_zipcode');
              $latitude = $this->input->post('latitude');
              $longitude = $this->input->post('longitude');
              $country_name = $this->input->post('country_name');
              // company logo
              $company_logo = $this->input->post('upload');          
              $this->Directory_company_model->update_company($id, $company_name, $company_desc, $company_website,
              $contact_number, $mobile, $fax_number, $contact_email, $company_logo, $province_id, $city_name);
              
              //die($this->Directory_company_model->db->last_query());
              $this->Directory_company_model->update_location($id, $physical_street, $physical_suburb, $physical_city,
              $physical_zipcode, $postal_street, $postal_suburb, $postal_city, $postal_zipcode,$latitude, $longitude, $country_name);
              $data['notification']="Company detail has been updated";    
            }
                        
        }
        $data['company'] = $this->Directory_company_model->get_company_detail($id);        
        $data['company_detail'] = 1;
		
		 $data['region_list']    = $this->_get_region_by_country(224);
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_company', $data);    
    }
    
    function products(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $serv = $param_array['service'];
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $services = $this->input->post('services');
            $this->Directory_company_model->update_services($id,$services);
            $data['notification']="Products/Services/Brands information updated";
        }
        /*
        switch($serv){
            case 'product' :
                $data['products_list'] = $this->Directory_company_model->get_services('products');
                $data['products'] = 1;
                break;
            case 'service' :
                $data['services_list'] = $this->Directory_company_model->get_services('services');
                $data['services'] = 1;
                break;
            case 'brands' :
                $data['brands_list'] = $this->Directory_company_model->get_services('brands');
                $data['brands'] = 1;
                break;
        }   
        */
        
        $data['services'] = 1;
        
        $data['services_list_print'] =  $this->Directory_company_model->get_services_print();
        
        $data['services_list_web'] =  $this->Directory_company_model->get_services_web();
        
        $company_services_list = $this->Directory_company_model->get_company_all_services($id);
        if(!empty($company_services_list)){
            $company_services = array();
            foreach($company_services_list as $key=>$value){
                $company_services[] = $value['id_services'];
            }
            $data['company_services'] = $company_services;
        }
        
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
       
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_services', $data);    
    }
    
    function branch(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['branch_list'] = $this->Directory_branch_model->get_branchs($id);
        $data['branch'] = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_branch', $data);    
    }
    
    function branch_create(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $submit_button = $this->input->post('submit');
        $data['postalCountry'] = '224' ;
        if($submit_button != ""){
            $this->form_validation->set_rules('branch_name', 'Branch name','required');
            $this->form_validation->set_rules('telephone', 'Telephone number','required');
            $this->form_validation->set_rules('email', 'email','valid_email');
            $this->form_validation->set_rules('physical_street', 'Physical street address','required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address','required');
            $this->form_validation->set_rules('physical_city', 'Physical city address','required');
            //$this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            if($this->form_validation->run() == true){
                $name = $this->input->post('branch_name');
                $telephone = $this->input->post('telephone');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $contact = $this->input->post('contact_person');
                $branch_id = $this->Directory_branch_model->add_branch($id ,$name,$telephone,$fax,$email,$contact);
                // location data
                $physical_street = $this->input->post('physical_street');
                $physical_suburb = $this->input->post('physical_suburb');
                $physical_city = $this->input->post('physical_city');
                $physical_zipcode = $this->input->post('physical_zipcode');
                $postal_street = $this->input->post('postal_street');
                $postal_suburb = $this->input->post('postal_suburb');
                $postal_city = $this->input->post('postal_city');
                $postal_zipcode = $this->input->post('postal_zipcode');
                $latitude = $this->input->post('latitude');
                $longitude = $this->input->post('longitude');

                $provinceName = $this->input->post('province_name');
                $cityName = $this->input->post('city_town_1');
                $postalCountry = $this->input->post('postal_country');
                
                $this->Directory_branch_model->addPostalCountry($postalCountry);
                $this->Directory_branch_model->addCityAndProvinceName($cityName,$provinceName);
                
                $data['debug'] = $this->Directory_branch_model->update_location($branch_id, $physical_street, $physical_suburb, $physical_city,
                $physical_zipcode, $postal_street, $postal_suburb, $postal_city, $postal_zipcode, $latitude, $longitude);
                redirect('admin/security_directory/branch/id/'.$id);
            }
        }
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['countryList'] = $this->countryList;
        //$data['branch_list'] = $this->Directory_branch_model->get_branchs($id);
        $data['branch'] = 1;
        //echo "<pre><font color='#999999'>";print_r($_POST); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_branch_create', $data);    
    }
    
    function branch_edit(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $branch_id = $param_array['branch_id'];
        $branch_detail['postal_country_id']  = $branch_detail['postal_country_id'] == '' ? '224' : $branch_detail['postal_country_id'];
        $company = $this->Directory_company_model->get_company_detail($id);
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $this->form_validation->set_rules('branch_name', 'Branch name','required');
            $this->form_validation->set_rules('telephone', 'Telephone number','required');
            $this->form_validation->set_rules('email', 'email','valid_email');
            $this->form_validation->set_rules('physical_street', 'Physical street address','required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address','required');
            $this->form_validation->set_rules('physical_city', 'Physical city address','required');
            $this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            if($this->form_validation->run() == true){
                $name = $this->input->post('branch_name');
                $telephone = $this->input->post('telephone');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $contact = $this->input->post('contact_person');
                $data['debug2'] = $this->Directory_branch_model->update_branch($company['id'],$branch_id ,$name,$telephone,$fax,$email,$contact);
                // location data
                $physical_street = $this->input->post('physical_street');
                $physical_suburb = $this->input->post('physical_suburb');
                $physical_city = $this->input->post('physical_city');
                $physical_zipcode = $this->input->post('physical_zipcode');
                $postal_street = $this->input->post('postal_street');
                $postal_suburb = $this->input->post('postal_suburb');
                $postal_city = $this->input->post('postal_city');
                $postal_zipcode = $this->input->post('postal_zipcode');
                $latitude = $this->input->post('latitude');
                $longitude = $this->input->post('longitude');
                
                $provinceName = $this->input->post('province_name');
                $cityName = $this->input->post('city_town_1');
                $postalCountry = $this->input->post('postal_country');
                
                $this->Directory_branch_model->addPostalCountry($postalCountry);
                $this->Directory_branch_model->addCityAndProvinceName($cityName,$provinceName);

                $data['debug1'] = $this->Directory_branch_model->update_location($branch_id, $physical_street, $physical_suburb, $physical_city,
                $physical_zipcode, $postal_street, $postal_suburb, $postal_city, $postal_zipcode, $latitude, $longitude);
                redirect('admin/security_directory/branch/id/'.$id);
            }
        }
        $data['company'] = $company;
        $data['branch_detail'] = $this->Directory_branch_model->get_branch_detail($branch_id);
        $data['branch'] = 1;
        $data['edit'] = 1;
        $data['branch_id'] = $branch_id;
        $data['countryList'] = $this->countryList;
         
        //echo "<pre><font color='#999999'>";print_r($_POST); echo "</font></pre>";
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_branch_create', $data);    
    }
    
    function branch_delete(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $branch_id = $param_array['branch_id'];
        $this->Directory_branch_model->delete_branch($branch_id ,$id);
        redirect('admin/security_directory/branch/id/'.$id);
    }
    
    function photo(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $submit_button = $this->input->post('submit');
        if($submit_button == "submit"){
            
            $file_name =  $this->input->post('upload');
            $file_caption =  $this->input->post('news_caption');
            if ($file_name){
                for ($i = 0; $i < count($file_name); $i++) {                    
                    $this->Directory_photo_model->add_photo($id, $file_name[$i],$file_caption[$i]);
                    $image .= $file_name[$i]." has been added to gallery <br />";
                }
            }
            $data['notification']= $image;                 
        }
        if($submit_button == "delete"){
            $photo_id =  $this->input->post('photo_id');            
            if ($photo_id){
                for ($i = 0; $i < count($photo_id); $i++) {
                    $this->Directory_photo_model->delete_photo($photo_id[$i]);                                  
                }
            }        
        }
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['company_photo'] = $this->Directory_photo_model->get_photo($id);        
        $data['photo'] = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_photo', $data);    
    }
    
    function news(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['news_list'] = $this->Directory_news_model->get_news($id);        
        $data['news'] = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_news', $data);    
    }
    
    function news_create(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $this->form_validation->set_rules('heading', 'Heading','required');
            $this->form_validation->set_rules('story', 'Story','required');
            if($this->form_validation->run() == true){
                $heading = $this->input->post('heading');
                $story = $this->input->post('story');
                $news_id = $this->Directory_news_model->add_news($id,$heading,$story);
                $file_name =  $this->input->post('upload');
                $file_caption =  $this->input->post('news_caption');
                if ($file_name){
                    for ($i = 0; $i < count($file_name); $i++) {                    
                        $this->Directory_news_model->insert_news_image($news_id, $file_name[$i],$i+1,$file_caption[$i]);              
                    }
                }
                redirect('admin/security_directory/news/id/'.$id);
            }
        }
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['news'] = 1;
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_news_create', $data);        
    }
    
    function news_edit(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $news_id = $param_array['news_id'];
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $this->form_validation->set_rules('heading', 'Heading','required');
            $this->form_validation->set_rules('story', 'Story','required');
            if($this->form_validation->run() == true){
                $heading = $this->input->post('heading');
                $story = $this->input->post('story');
                $this->Directory_news_model->update_news($news_id, $heading, $story);
                $file_name =  $this->input->post('upload');
                $file_caption =  $this->input->post('news_caption');
                if ($file_name){
                    for ($i = 0; $i < count($file_name); $i++) {                    
                        $this->Directory_news_model->insert_news_image($news_id, $file_name[$i],$i+1,$file_caption[$i]);              
                    }
                }
                redirect('admin/security_directory/news/id/'.$id);
            }
        }
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['news_detail'] = $this->Directory_news_model->get_news_detail($news_id);    
        $data['news'] = 1;
        $data['edit'] = 1;
        $data['news_id'] = $news_id;
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_news_create', $data);    
    }
    
    function news_delete(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        } */       
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $news_id = $param_array['news_id'];
        $this->Directory_news_model->delete_news($news_id ,$id);
        redirect('admin/security_directory/news/id/'.$id);
    }
    
    function company_delete(){
        /*if(!isset($_SESSION["admin_id"]) or empty($_SESSION["admin_username"])){
            redirect('../admin');
        }*/        
        $param_array = $this->uri->uri_to_assoc(4);
        $id = $param_array['id'];
        $submit_button = $this->input->post('submit');
        if($submit_button != ""){
            $confirm_delete = $this->input->post('confirm_delete');
            if($confirm_delete == 'delete'){                
                $this->Directory_company_model->delete_company($id);                
                redirect('admin/security_directory');
            }    
        }
        
        $data['company'] = $this->Directory_company_model->get_company_detail($id);
        $data['delete'] = 1;
        //echo "<pre><font color='#999999'>";print_r($_POST); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/admin/directory/directory_company_delete', $data);
    }
    
    function check_email($val) {
        if (!$this->Directory_user_model->check_available_email($val,$this->company_id)) {
            $this->form_validation->set_message('check_email', '%s ' . $val .
                ' is already on our system, please use another %s');
            return false;
        } else {
            return true;
        }
    }
	
	function _get_region_by_country($country_id){
    $this->db->where('country', $country_id);
    $this->db->where('adm1code !=', '');
    $query = $this->db->get('region');
    return $query->result_array();
  }
    
}
?>
